<?php
/* OpenBIBLIO -- A library administration web interface
 * Copyright (C) 2002-2005 Fr&eacute;d&eacute;ric Descamps <lefred@tiscali.be>
 * Copyright (C) 2011-     Laurent Luyssen   <big.lol13@gmail.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */

if($_GET['id']) $id=$_GET['id'];
else $id=$_POST['id'];
$sw=$_POST['sw'];

if(!$id)
{
        include("includes/search.inc");
}
else
{
    if ($sw!=1)
    {
    	$row = get_book($id);
		if (!$row) {
			print "PROBLEME : " .biblio_db_error();
		}
		else {
			$liv_id = $row['id'];
           	$liv_nom =  $row['nom'];
           	$liv_auteur =  $row['auteur'];
            $liv_genre_ID =  $row['genre_ID'];
           	$liv_local_ID =  $row['location_ID'];
           	$liv_etat_ID =  $row['etat_ID'];
           	$liv_ISBN =  $row['ISBN'];
           	$liv_num =  $row['num'];
            $liv_ed = $row['edition'];
           	$liv_date = $row['date_ed'];
           	$liv_col = $row['collection'];
           	$liv_temp = $row['temp'];
           	$liv_noout = $row['noout'];
           	$liv_resume = $row['resume'];
           	$liv_motclefs = $row['motclefs'];
           	$requete = "select * from genres where id=$liv_genre_ID";
           	if($resultat2 = biblio_db_query($requete))
           	while ($row2 = biblio_db_fetch_array($resultat2))
           	{
           		$genre = $row2['genre'];
            }
		}		
        ?>
        <CENTER><H3>
	<?php echo _("Modifier le livre"); ?>
	</H3>
	<img src=images/covers/<?php echo $id.".jpg"; ?> align=right>
        <FORM enctype=multipart/form-data action=admin.php METHOD=POST>
        <TABLE BORDER=0>
        <TR>
        <TD>
	<?php echo _("Titre"); ?>
	:</TD><TD><INPUT TYPE="text" NAME="liv_nom" size=50 value="<?php echo $liv_nom; ?>" ></TD>
        <TR>
        <TD>
	<?php echo _("Num&eacute;ro"); ?>
	:</TD><TD><INPUT TYPE="text" NAME="liv_num" size=4 value="<?php echo $liv_num; ?>" ></TD>
        <TR>
        <TD>
	<?php echo _("Auteur"); ?>
	:</TD><TD><INPUT TYPE="text" NAME="liv_auteur" size=30 value="<?php echo $liv_auteur; ?>"></TD>
        <TR>
        <TD>
	<?php echo _("Collection"); ?>
	:</TD><TD><INPUT TYPE="text" NAME="liv_col" size=30 value="<?php echo $liv_col; ?>"></TD>
        <TR>
        <TD>
	<?php echo _("Edition"); ?>
	:</TD><TD><INPUT TYPE="text" NAME="liv_ed" size=15 value="<?php echo $liv_ed; ?>"></TD>
        <TR>
        <TD>
	<?php echo _("Date"); ?>
	:</TD><TD><INPUT TYPE="text" NAME="liv_date" size=10 value="<?php echo $liv_date; ?>"></TD>
        <TR>
        <TD>
	<?php echo _("Genre"); ?>
	:</TD><TD><select NAME = "liv_genre">
<?php
	$genres = get_genres();
	if ($genres) {
		foreach ($genres as $key => $value) {
			print "<option VALUE=\"$key\"";
			if ($key == $liv_genre_ID)
				echo ' selected="selected"';
			echo ">".$value."</option>";
		}
	}
?>
        </select></TD>
        <TR>
        <TD>
	<?php echo _("R&eacute;sum&eacute;"); ?>
	:</TD><TD><TEXTAREA name="liv_resume" cols="50" rows="5">
        <?php echo "$liv_resume"; ?>
        </TEXTAREA></TD>
        <TR>
        <TD>
	<?php echo _("Mots cl&eacute;"); ?>
	:</TD><TD><INPUT TYPE="text" NAME="liv_motclefs" size=30 maxlength=200 value="<?php echo $liv_motclefs; ?>">
        <small>(<?php echo _("s&eacute;par&eacute;s par un espacement"); ?>)</small></TD>
        <TR>
        <TD>
	<?php echo _("Etat"); ?>
	:</TD><TD><select NAME = "liv_etat">
        <?php
        $requete = "SELECT * FROM etats order by value, etat"; 
	if(!($resultat = biblio_db_query($requete)))
	{ print "PROBLEME : " .biblio_db_error(); }
	else 
	{
		while ($row = biblio_db_fetch_array($resultat)) 
		{      
		        $etat_id2 = $row['id'];           
			$etat_l =  $row['etat'];                                
			if ($liv_etat_ID==$etat_id2) { $str = "selected"; }
			else { $str="";}
			print "<option VALUE=\"$etat_id2\" $str>$etat_l";
		}
	}                                
        
        ?>
        </select></TD>
        <TR>
        <TD>
	<?php echo _("Location"); ?>
	:</TD><TD><select NAME = "liv_location">
<?php
	$locations = get_locations();
	if ($locations) {
		foreach ($locations as $id => $location) {
			if ($liv_local_ID==$id)
				echo '<option VALUE="'.$id.'" selected="selected">';
			else
				echo '<option VALUE="'.$id.'">';
			echo $location.'</option>';
		}
	}                                  
?>
        </select></TD>
        <TR>
        <?php if($liv_temp == 1) {$str="checked";} ?>
        <TD>
	<?php echo _("Temporaire"); ?>
	:</TD><TD><INPUT TYPE="checkbox" NAME = "temp" value=1 <?php echo $str; ?>></TD>
        <TR>
        <?php if($liv_noout == 1) {$str2="checked";} ?>
        <TD>
	<?php echo _("Ne peux sortir !"); ?>
	:</TD><TD><INPUT TYPE="checkbox" NAME = "noout" value=1 <?php echo $str2; ?>></TD>
        <TR>
        <TD>
	<?php echo _("ISBN"); ?>
	:</TD><TD><INPUT TYPE="text" NAME="liv_ISBN" size=30 value="<?php echo $liv_ISBN; ?>"></TD>
        </tr>
	<tr>
 	<TD>
	<?php echo _("Couverture"); ?>
	:</TD><TD><input type=file name="liv_cover"><i>
	<?php echo _("fichier"); ?> 
	</i><br>
	<input type="text" size=30" name="liv_url"><i>
	<?php echo _("ou url"); ?> 
	</i></TD>
	</TR>
        </TABLE><BR>
        <INPUT TYPE="hidden" NAME="user" value="<?php echo $user; ?>">
        <INPUT TYPE="hidden" NAME="action" value="<?php echo $action; ?>">
        <INPUT TYPE="hidden" NAME="type" value="<?php echo $type; ?>">
        <INPUT TYPE="hidden" NAME="sw" value="1">
        <INPUT TYPE="hidden" NAME="id" value="<?php echo $liv_id; ?>">        
        <input type="Submit" value="<?php echo _("Modifier"); ?>">
        </FORM>
        </CENTER>
        <?php
    }   
    else
    {   
      $temp=$_POST['temp'];
      $noout=$_POST['noout'];
      $liv_resume=$_POST['liv_resume'];
      $liv_motclefs=$_POST['liv_motclefs'];
      $liv_nom= format_title($_POST['liv_nom']);
      $liv_num=$_POST['liv_num'];
      $liv_auteur=format_author($_POST['liv_auteur']);
      $liv_col=$_POST['liv_col'];
      $liv_genre=$_POST['liv_genre'];
      $liv_location=$_POST['liv_location'];
      $liv_ed=$_POST['liv_ed'];
      $liv_etat=$_POST['liv_etat'];
      $liv_ISBN=$_POST['liv_ISBN'];
      $liv_date=$_POST['liv_date'];

      if($temp != 1)
      { $temp = 0;}
      if ($noout != 1) { $noout = 0;}
      $requete = "update livres set resume=\"$liv_resume\", motclefs=\"$liv_motclefs\", nom=\"$liv_nom\", auteur=\"$liv_auteur\",collection=\"$liv_col\",genre_ID=$liv_genre,location_ID=$liv_location,edition=\"$liv_ed\",etat_ID=$liv_etat,ISBN='$liv_ISBN',temp=$temp,noout=$noout,date_ed='$liv_date',num='$liv_num' where id=$id";
      if(!($resultat = biblio_db_query($requete)))
      { echo "PROBLEME : $requete: " .biblio_db_error(); exit;}  
      put_log(3,$requete,$user);
      echo "<CENTER><BR>";
      echo _("Le livre a &eacute;t&eacute; modifi&eacute;.");
      echo "<br></center>";
      $filename = $id . ".jpg";
      $cover_path=get_coverspath();
      if ($_FILES['liv_cover']['tmp_name'])
      {
	system("cp ". $_FILES['liv_cover']['tmp_name'] ." $cover_path/$filename");
        system("cd $cover_path; mogrify -resize 200x200 $filename");
      }
      elseif ( preg_match ("/http\:\/\//i",$_POST['liv_url']))
      {
 	system ("wget " . $_POST['liv_url'] ." -O $cover_path/$filename");
        system("cd $cover_path; mogrify -resize 200x200 $filename");
      }
    } 
}
?>
<br><br>
<div>
<span id=left-text>
<A HREF="admin.php?menu=1">
<?php echo _("retour menu administration"); ?>
</A><BR>
<A HREF="index.php">
<?php echo _("retour menu principal"); ?>
</A></span>
</div>
<br>

